/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.elka.lisiecki.pobr;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
    dtd="-//pl.elka.lisiecki.pobr//PobrWindow//EN",
    autostore=false
)
public final class PobrWindowTopComponent extends TopComponent {

    private static PobrWindowTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";

    private static final String PREFERRED_ID = "PobrWindowTopComponent";

    public PobrWindowTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(PobrWindowTopComponent.class, "CTL_PobrWindowTopComponent"));
        setToolTipText(NbBundle.getMessage(PobrWindowTopComponent.class, "HINT_PobrWindowTopComponent"));
//        setIcon(ImageUtilities.loadImage(ICON_PATH, true));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        imageOutput = new javax.swing.JLabel();

        setLayout(new java.awt.GridLayout());

        org.openide.awt.Mnemonics.setLocalizedText(imageOutput, org.openide.util.NbBundle.getMessage(PobrWindowTopComponent.class, "PobrWindowTopComponent.imageOutput.text")); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(imageOutput, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(imageOutput, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
                .addContainerGap())
        );

        add(mainPanel);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel imageOutput;
    private javax.swing.JPanel mainPanel;
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized PobrWindowTopComponent getDefault() {
        if (instance == null) {
            instance = new PobrWindowTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the PobrWindowTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized PobrWindowTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(PobrWindowTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof PobrWindowTopComponent) {
            return (PobrWindowTopComponent) win;
        }
        Logger.getLogger(PobrWindowTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
        try {
            System.out.println("Opening image");
            BufferedImage image = ImageIO.read(new File("/home/mlisieck/projekty/POBR/pobr cyfra+ aparat/photos/DSC_1043.JPG"));
            

            BufferedImage imageDest = new BufferedImage(1000, 800, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = imageDest.createGraphics();
            AffineTransform transform = AffineTransform.getScaleInstance(1000.0/image.getWidth(), 800.0/image.getHeight());
            graphics.drawRenderedImage(image, transform);

            Icon icon = new ImageIcon(imageDest);
            imageOutput.setLocation(0, 0);
            imageOutput.setText("");
//            imageOutput.setSize(mainPanel.getWidth(), mainPanel.getHeight());
//            imageOutput.set
            System.out.println("w: "+icon.getIconWidth()+" | h: "+icon.getIconHeight());

            imageOutput.setIcon(icon);
            System.out.println("Image showed");
            mainPanel.setBackground(Color.BLUE);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    Object readProperties(java.util.Properties p) {
        PobrWindowTopComponent singleton = PobrWindowTopComponent.getDefault();
        singleton.readPropertiesImpl(p);
        return singleton;
    }

    private void readPropertiesImpl(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }
}
